package se.webbzon.oschi01.action;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

import se.webbzon.oschi01.gui.ApplicationIconSheet;
import se.webbzon.oschi01.gui.ApplicationIcon;
import se.webbzon.oschi01.gui.NBTTextFieldDialog;
import se.webbzon.oschi01.nbt.Tag;
import se.webbzon.oschi01.nbt.Tags;
import se.webbzon.oschi01.singleton.Singletons;
import se.webbzon.oschi01.utils.Converter;

public class NBTOpenAction extends AbstractAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4457304338331573132L;
	
	private final Tag<?> nbtTag;
	
	public NBTOpenAction(Tag<?> nbtTag) {
		this.nbtTag = nbtTag;
		putValue(NAME, "Open");
		putValue(SMALL_ICON, Singletons.instance(ApplicationIconSheet.class).getIcon(ApplicationIcon.Open));
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override public void actionPerformed(ActionEvent arg0) {
		Converter<String,?> conv = null;
		if (Tags.isByteTag(nbtTag))
			conv = new ByteConverter();
		else if (Tags.isShortTag(nbtTag))
			conv = new ShortConverter();
		else if (Tags.isIntTag(nbtTag))
			conv = new IntegerConverter();
		else if (Tags.isLongTag(nbtTag))
			conv = new LongConverter();
		else if (Tags.isFloatTag(nbtTag))
			conv = new FloatConverter();
		else if (Tags.isDoubleTag(nbtTag))
			conv = new DoubleConverter();
		else if (Tags.isStringTag(nbtTag))
			conv = new StringConverter();
		
		if (conv != null) {
			new NBTTextFieldDialog(nbtTag,conv).setVisible(true);
		}
	}
	
	private static final class ByteConverter implements Converter<String,Byte> {

		@Override public Byte convert(String in) {
			return Byte.parseByte(in);
		}
		
	}
	
	private static final class ShortConverter implements Converter<String,Short> {

		@Override public Short convert(String in) {
			return Short.parseShort(in);
		}
		
	}
	
	private static final class IntegerConverter implements Converter<String,Integer> {

		@Override public Integer convert(String in) {
			return Integer.parseInt(in);
		}
		
	}
	
	private static final class LongConverter implements Converter<String,Long> {

		@Override public Long convert(String in) {
			return Long.parseLong(in);
		}
		
	}
	
	private static final class FloatConverter implements Converter<String,Float> {

		@Override public Float convert(String in) {
			return Float.parseFloat(in);
		}
		
	}
	
	private static final class DoubleConverter implements Converter<String,Double> {

		@Override public Double convert(String in) {
			return Double.parseDouble(in);
		}
		
	}
	
	private static final class StringConverter implements Converter<String,String> {

		@Override public String convert(String in) {
			return in;
		}
		
	}

}
